package biz.ezcom.hibernate.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import biz.ezcom.hibernate.pojo.Animal;
import biz.ezcom.hibernate.pojo.Bird;
import biz.ezcom.hibernate.pojo.Pig;
/**
 * 	多张子表,继承的层次结构清楚，union查询效率低
 */
public class Client {
	public static String UUID_BIRD = "";
	public static String UUID_PIG = "";
	@Test
	public void testSave(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			Bird bird = new Bird();
			bird.setHeight("birdheight");
			bird.setName("bird");
			session.save(bird);
			UUID_BIRD = bird.getId();

			Pig pig = new Pig();
			pig.setName("pig");
			pig.setWeight("pigweight");
			session.save(pig);
			UUID_PIG = pig.getId();
			
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			tx.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	@Test
	public void testLoadPig(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			Pig pig = (Pig)session.load(Pig.class, UUID_PIG);
			System.out.println(pig.getName());
			System.out.println(pig.getWeight());
			
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			tx.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	@Test
	public void testLoadAnimal(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			Animal pig = (Animal)session.load(Pig.class, UUID_PIG);
			System.out.println(pig.getName());
			
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			tx.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	@Test
	public void testQuery(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			Query query = session.createQuery("from Animal");//Query 　查询有lazy
			@SuppressWarnings("unchecked")
			List<Animal> list = (List<Animal>)query.list(); //发SQL,bird union pig
			for(Animal animal : list){
				System.out.println();
				if(animal instanceof Pig) {
					Pig pig = (Pig)animal;
					System.out.println("pig.id: " + pig.getId());
					System.out.println("pig.name: " + pig.getName());
					System.out.println("pig.weight: " + pig.getWeight());
				}else if(animal instanceof Bird){
					Bird bird = (Bird)animal;
					System.out.println("bird.id: " + bird.getId());
					System.out.println("bird.name: " + bird.getName());
					System.out.println("bird.height: " + bird.getHeight());
				}else{
					System.out.println("animal.id: " + animal.getId());
					System.out.println("animal.name: " + animal.getName());
				}
			}
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			tx.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
}
